package it.polimi.swim.servlet;

import it.polimi.swim.entity.SystemUser;
import it.polimi.swim.session.FriendManagerBeanRemote;
import it.polimi.swim.session.SystemUserManagerBeanRemote;

import java.io.IOException;
import java.util.List;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class FriendshioRequestNotificationServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FriendshioRequestNotificationServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try{
			Properties env = new Properties();
			env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
			env.put(Context.PROVIDER_URL,"localhost:1099");
			InitialContext jndiContext = new InitialContext(env);
			HttpSession session = request.getSession();
			int id=(Integer) session.getAttribute("id");
			
			
			InitialContext ctx = new InitialContext();
			Object ref = ctx.lookup("SystemUserManagerBean/remote");
			SystemUserManagerBeanRemote systemUserManager = (SystemUserManagerBeanRemote) ref;
			Object ref2 = ctx.lookup("FriendManagerBean/remote");
			FriendManagerBeanRemote friendManager = (FriendManagerBeanRemote) ref2;
			
			//crea lista di richieste inviate
			List<SystemUser> friends = friendManager.showMyFriendshipRequest(id);
			request.setAttribute("myFriendList", friends);	
			//crea lista richieste ricevute
			List<SystemUser> friends2 = friendManager.showOtherFriendshipRequest(id);
			request.setAttribute("otherFriendRequestList", friends2);
									
			request.getRequestDispatcher("/friendRequest.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
